<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2019/11/8
 * Time: 11:26
 */

namespace App\Http\Controllers;


use App\Model\Location;
use App\Model\User;
use App\Notifications\InvoicePaid;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;


class TestController
{
    public function index()
    {
        $location = Location::find(1);
        $user     = User::find(1);
        $user->notify(new InvoicePaid($location));
    }

    public function getMessage()
    {
        $user         = User::find(1);
        $notification = $user->notifications()->find('a73c5c33-11b8-44d1-8cb0-2e7f77aac30f');
        $notification->markAsRead();
        return $notification;

    }

    public function mongodb()
    {
        $connection = \App\Mongodb::connection('users');
        //增
//        $connection->insert(['_id'=>1,'name'=>'张三']);
        //改
//        $connection->where('_id',1)->update(['name'=>'张天']);
        //查
//        $result= $connection->find(2);
        $result = $connection->where('name', 'like', '%张%')->get();
//        $result= $connection->first();
        //删
//        $result = $connection->where('_id',2)->delete();
        return $result;

    }

    public function redis()
    {
        $result = Redis::get('test');
        $result = json_decode($result, true);
        $arr    = [
            'code' => 0,
            'msg'  => '',
            'data' => [
                'list' => array_values($result)
            ]
        ];
        return $arr;
    }

    public function ini()
    {
        $result = Redis::get('test');
        $result = json_decode($result, true);
        $mine   = $result[$_GET['id']];
        $arr    = [
            'code' => 0,
            'msg'  => '',
            'data' => [
                'mine'   => $mine,
                'friend' => [
                    [
                        'groupname' => '同学',
                        'id'        => 1,
                        'list'      => [
                            [
                                "username" => "蜡笔小新",
                                "id"       => "2",
                                "avatar"   => "http://pic1.zhimg.com/50/v2-d5d077f05fdaadf654ab43c85ccd7db2_hd.jpg",
                                "sign"     => "让天下没有难写的代码",
                                "status"   => 'online'
                            ],
                            [
                                "username" => "马云",
                                "id"       => "3",
                                "avatar"   => "http://tp4.sinaimg.cn/2145291155/180/5601307179/1",
                                "sign"     => "让天下没有难写的代码",
                                "status"   => 'offline'
                            ]
                        ]
                    ]
                ],
                'group'  => [
                    [
                        'groupname' => '亿万富豪交流群',
                        'id'        => 1,
                        'avatar'    => 'http://pic.51yuansu.com/pic3/cover/02/47/36/59e5e5be5df32_610.jpg'
                    ]
                ]
            ]
        ];

        return $arr;
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');
        $disk = 'public';
        try {
            $FileType = $file->getClientOriginalExtension(); //获取文件后缀
//            //文件后缀
//            $fileTypes = ['webp', 'jpg', 'png', 'jpeg', 'bmp', 'gif', 'txt', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'rar', 'zip', 'tar'];
//            if (!in_array(strtolower($FileType), $fileTypes)) {
//                return $this->error('上传文件不合法');
//            }
//            //如果是图片
//            if (in_array(strtolower($FileType), ['webp', 'jpg', 'png', 'jpeg'])) {
//                $file_saze = $file->getClientSize();
//                if ($file_saze > (1024 * 1024 * 2)) {
//                    return $this->error('上传图片不能超过2M');
//                }
//            } else {
//                $file_saze = $file->getClientSize();
//                if ($file_saze > (1024 * 1024 * 10)) {
//                    return $this->error('上传文件不能超过10M');
//                }
//            }
            $path = Storage::disk($disk)->putFile(
                'files', $file
            );

            $url = Storage::disk($disk)->url($path);
//            //如果是图片
//            if (($is_keep_name != 1) && in_array(strtolower($FileType), ['webp', 'jpg', 'png', 'jpeg'])) {
//                $imagePath = config("filesystems.disks.{$disk}.root") . '/' . $path;
//                $this->cutImage($imagePath, 1000, 500);
//            }
            return [
                'code' => 0,
                'msg'  => '',
                'data' => [
                    'src' => $url
                ]
            ];
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function groupUsers(){
        $id = request()->get('id');
        //设置主面板信息
        $friend = Redis::get('admin_role_users');
        $friend = json_decode($friend, true);

        $arr    = [
            'code' => 0,
            'msg'  => '',
            'data' => [
                'list' => array_values($friend[$id]['list'])
            ]
        ];
        return $arr;
    }
}